﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.business.stamm;
using personalplaner.common.mvvm;
using System.Windows;
using System.Windows.Input;
using personalplaner.common.mvvm.commands;
using System.Windows.Controls;
using personalplaner.gui.common.views;
using personalplaner.servicemodel.dataaccess;
using personalplaner.gui.common.viewmodels.models;

namespace personalplaner.gui.common.viewmodels
{
    public class InitWizardViewModel : BaseViewModel
	{
        public InitWizardViewModel(DependencyObject parent)
			: base(parent)
		{
		}

		#region Commands

		#region General

		ICommand _okcommand;
		public ICommand OkCommand
		{
			get
			{
				if (_okcommand == null)
					_okcommand = new RelayCommand(OnOkCommand, CanExecuteOk);
				return _okcommand;
			}
		}

		private void OnOkCommand(object parameter)
		{
			var wnd = Parent as Window;
			if (wnd == null)
				throw new Exception();

            //save the data
            var da = GetService<ICommonDataAccess>();
            // create the default mandant
            da.SetMandant("pa", "personalplaner", "The initial mandant for the personalplaner");

            personalplaner.common.SettingsProvider.Settings.Mandant = da.GetActiveMandid();

            //  create the new year
            da.CreateNewYear(YearModel.SelectedYear);

            // save the teams
            var ada = GetService<IAbteilungDataAccess>();
            foreach (var team in TeamModel.Abteilungen)
                ada.SetAbteilung(team);

            // save the anstellungen
            var ansda = GetService<IAnstellungDataAccess>();
            foreach (var anstellung in AnstellungModel.Anstellungen)
                ansda.SetAnstellung(anstellung);

            // get the new abteilungen
            var tmpabt = ada.GetAbteilungen();
            if (tmpabt == null)
                tmpabt = new List<Abteilung>();

            var tmpanst = ansda.GetAnstellungen();
            if (tmpanst == null)
                tmpanst = new List<Anstellung>();

            // save the personen
            foreach (var person in PersonModel.Personen)
            {
                var additionalInfoModel = PersonModel.AdditionalInfos[person];
                person.ArbeitsPensum = additionalInfoModel.Pensum;
                // get the selected anstellung
                var anstellung = (from a in tmpanst where a.Name == additionalInfoModel.Anstellung.Name select a).FirstOrDefault() ?? new Anstellung();

                var pda = GetService<IPersonDataAccess>();
                int prsId = pda.SavePerson(person, additionalInfoModel.Von, additionalInfoModel.Bis, anstellung.AnstellungID);

                //Abteilungen
                var pada = GetService<IAbteilungDataAccess>();
                foreach (var abt in PersonModel.PersonAbteilungen[person].AbteilungenZugeteilt)
                {
                    var abteilung = (from a in tmpabt where a.Name == abt.Name select a).FirstOrDefault();
                    if (abteilung != null)
                        pada.SetPersonAbteilung(prsId, abteilung.AbteilungID, abt.Von, abt.Bis, abt.Prozent);
                }
            }
            
			wnd.DialogResult = true;
			wnd.Close();
		}

		private bool CanExecuteOk(object parameter)
		{
			RaisePropertyChanged("IsEndOfWizard");
			return IsEndOfWizard;// && PersonAbteilungModel.AbteilungenZugeteilt.Count > 0;
		}

		ICommand _cancelcommand;
		public ICommand CancelCommand
		{
			get
			{
				if (_cancelcommand == null)
					_cancelcommand = new RelayCommand(OnCancelCommand);
				return _cancelcommand;
			}
		}

		private void OnCancelCommand(object parameter)
		{
			var wnd = Parent as Window;
			if (wnd == null)
				throw new Exception();

			wnd.DialogResult = false;
			wnd.Close();
		}

		ICommand _nextcommand;
		public ICommand NextCommand
		{
			get
			{
				if (_nextcommand == null)
					_nextcommand = new RelayCommand(OnNext, CanExecuteNext);
				return _nextcommand;
			}
		}

		private void OnNext(object parameter)
		{
			if (IsEndOfWizard)
				return;

			if (SelectedView is InitWizardYearView)
			{
			    // YearModel
			}
			else if (SelectedView is InitWizardTeamView)
			{
				if (TeamModel.Abteilungen.Count <= 0)
				{
					MessageBox.Show(Window.GetWindow(Parent), Properties.Resources.TeamErfassen,
					                Properties.Resources.Anstellungen, MessageBoxButton.OK, MessageBoxImage.Exclamation);
					return;
				}

				PersonModel.Abteilungen.Clear();
				foreach (var abt in TeamModel.Abteilungen)
					PersonModel.Abteilungen.Add(abt);
			}
			else if (SelectedView is InitWizardAnstellungView)
			{
				if (AnstellungModel.Anstellungen.Count <= 0)
				{
					MessageBox.Show(Window.GetWindow(Parent), Properties.Resources.AnstellungErfassen, Properties.Resources.Anstellungen, MessageBoxButton.OK, MessageBoxImage.Exclamation);
					return;
				}

				PersonModel.Anstellungen.Clear();
				foreach (var anst in AnstellungModel.Anstellungen)
					PersonModel.Anstellungen.Add(anst);

                if (PersonModel.Anstellungen.Count > 0)
                    PersonModel.AdditionalInfoModel.Anstellung = PersonModel.Anstellungen[0];
			}
			else if (SelectedView is InitWizardPersonView)
			{

			}
			else if (SelectedView is InitWizardEndView)
			{
			}

			for (int i = 0; i < Views.Count; i++)
			{
				if (Views[i] == SelectedView)
				{
					SelectedView = Views[i + 1];
					break;
				}
			}
		}

        private bool CanExecuteNext(object parameter)
        {
            if (SelectedView == Views[5])
                return false;

            if (SelectedView is InitWizardYearView)
            {
                // YearModel
            }
            else if (SelectedView is InitWizardTeamView)
            {
                if (TeamModel.Abteilungen.Count <= 0)
                    return false;
            }
            else if (SelectedView is InitWizardAnstellungView)
            {
                if (AnstellungModel.Anstellungen.Count <= 0)
                    return false;
            }
            else if (SelectedView is InitWizardPersonView)
            {
                if (PersonModel.Personen.Count <= 0)
                    return false;
            }
            else if (SelectedView is InitWizardEndView)
            {
            }

            return true;
        }

		ICommand _previouscommand;
		public ICommand PreviousCommand
		{
			get
			{
				if (_previouscommand == null)
					_previouscommand = new RelayCommand(OnPrevious, parameter => SelectedView != Views[0]);
				return _previouscommand;
			}
		}

		private void OnPrevious(object parameter)
		{
			for (int i = 0; i < Views.Count; i++)
			{
				if (Views[i] == SelectedView)
				{
					SelectedView = Views[i - 1];
					break;
				}
			}
		}

		#endregion

		#endregion

		#region Properties

		#region Wizard

		IList<UserControl> _views;
    	public IList<UserControl> Views
    	{
    		get
    		{
    			if (_views == null)
    			{
					_views = new List<UserControl>
    				         {
    				         	new InitWizardWelcomeView(),
    				         	new InitWizardYearView
    				         	{
    				         		DataContext = YearModel
    				         	},
    				         	new InitWizardTeamView
                                {
                                    DataContext = TeamModel
                                },
    				         	new InitWizardAnstellungView
								{
									DataContext = AnstellungModel
								},
    				         	new InitWizardPersonView
								{
									DataContext = PersonModel
								},
                                new InitWizardEndView()
    				         };
    			}
    			return _views;
    		}
    	}

    	UserControl _selectedView;
		public UserControl SelectedView
		{
			get
			{
				if (_selectedView == null)
					_selectedView = Views[0];
				return _selectedView;
			}
			set
			{
				_selectedView = value;
				RaisePropertyChanged("SelectedView");
			}
		}

		public bool IsEndOfWizard
		{
			get
			{
				return SelectedView == Views[5];
			}
		}

		#endregion

        #region Models

        InitWizardYearModel _yearModel;
        public InitWizardYearModel YearModel
        {
            get
            {
                if (_yearModel == null)
                    _yearModel = new InitWizardYearModel();
                return _yearModel;
            }
        }

        InitWizardTeamModel _teamModel;
        public InitWizardTeamModel TeamModel
        {
            get
            {
                if (_teamModel == null)
                    _teamModel = new InitWizardTeamModel();
                return _teamModel;
            }
        }

		InitWizardAnstellungModel _anstellungModel;
    	public InitWizardAnstellungModel AnstellungModel
    	{
    		get
    		{
    			if (_anstellungModel == null)
    				_anstellungModel = new InitWizardAnstellungModel();
    			return _anstellungModel;
    		}
    	}

		InitWizardPersonModel _personModel;
    	public InitWizardPersonModel PersonModel
    	{
    		get
    		{
    			if (_personModel == null)
    				_personModel = new InitWizardPersonModel();
    			return _personModel;
    		}
    	}


    	#endregion

        #endregion
    }
}
